package com.xpgk.common.kit;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

public class PubKit {
	// 判断字符串是否为空或长度为0
	public static boolean isBlank(Object str) {
		if (str == null)
			return true;
		if (str instanceof String)
			return ((String) str).length() == 0;
		return false;
	}

	// 判断字符串中是否有为空或长度为0串
	public static boolean hasBlank(String... texts) {
		for (int i = 0; i < texts.length; i++) {
			if (texts[i] == null || texts[i].isEmpty())
				return true;
		}
		return false;
	}

	// 判断T中是否有为空
	@SafeVarargs
	public static <T> boolean hasNull(T... texts) {
		for (int i = 0; i < texts.length; i++) {
			if (texts[i] == null)
				return true;
		}
		return false;
	}

	// 如果o为空，返回defaulT否则返回o
	public static <T> T ifNull(T o, T defaulT) {
		return o == null ? defaulT : o;
	}

	// 返回当前日期
	public static String Date() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(new Date());
	}

	// 返回当前日期及时间
	public static String Time() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(new Date());
	}

	// 返回指定格式的当前日期
	public static String Date(String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(new Date());
	}

	// 返回指定格式的日期
	// "yyyy-MM-dd HH:mm:ss"
	// "yyyy-MM-dd" format为空时默认
	public static Date toDate(String date, String format) {
		if (isBlank(date))
			return null;
		try {
			format = ifNull(format, "yyyy-MM-dd");
			SimpleDateFormat df = new SimpleDateFormat(format);
			return df.parse(date);
		} catch (Exception ex) {
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static <K, V, K1, V1> Map<K, V> toMap(Map<K1, V1> map) {
		Map<K, V> m = new HashMap<K, V>();
		for (Entry<K1, V1> e : map.entrySet())
			m.put((K) e.getKey(), (V) e.getValue());
		return m;
	}

	// 将List<record>转换为Map
	@SuppressWarnings("unchecked")
	public static <K, V> Map<K, V> toMap(List<Record> records, String columnKeyName, String columnValueName) {
		Map<K, V> m = new HashMap<K, V>();
		for (Iterator<Record> iterator = records.iterator(); iterator.hasNext();) {
			Record record = (Record) iterator.next();
			m.put((K) record.get(columnKeyName), (V) record.get(columnValueName));
		}

		return m;
	}

	// 向records中添加列,列名为addColumnName,值为map中的value
	// 条件是records中列名为recordEqualsColumnName的列的值等于map中的key值
	@SuppressWarnings("unchecked")
	public static <K, V> void addColumnTo(List<Record> records, String recordEqualsColumnName, Map<K, V> map,
			String addColumnName) {
		for (Iterator<Record> iterator = records.iterator(); iterator.hasNext();) {
			Record record = (Record) iterator.next();
			record.set(addColumnName, map.get((K) record.get(recordEqualsColumnName)));
		}
	}

	// 得到纳秒 1毫秒=10^6纳秒
	public static long nanoTime() {
		return System.nanoTime();
	}

	public static Integer toInt(Object obj, Integer defaultValue) {
		if (obj == null)
			return defaultValue;
		String value = obj.toString();
		if (StrKit.isBlank(value))
			return defaultValue;
		value = value.trim();
		if (value.startsWith("N") || value.startsWith("n"))
			return -Integer.parseInt(value.substring(1));
		try {
			return Integer.parseInt(value);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	public static Integer toInt(Object obj) {
		return toInt(obj, null);
	}

	public static Long toLong(Object obj, Long defaultValue) {
		if (obj == null)
			return defaultValue;
		String value = obj.toString();
		if (StrKit.isBlank(value))
			return defaultValue;
		value = value.trim();
		if (value.startsWith("N") || value.startsWith("n"))
			return -Long.parseLong(value.substring(1));
		try {
			return Long.parseLong(value);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	public static Long toLong(Object obj) {
		return toLong(obj, null);
	}

	public static Double toDouble(Object obj, Double defaultValue) {
		if (obj == null)
			return defaultValue;
		String value = obj.toString();
		if (StrKit.isBlank(value))
			return defaultValue;
		value = value.trim();
		if (value.startsWith("N") || value.startsWith("n"))
			return -Double.parseDouble(value.substring(1));
		try {
			return Double.parseDouble(value);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	public static Double toDouble(Object obj) {
		return toDouble(obj, null);
	}

	// 得到异常的错误信息
	public static String getErrMessage(Exception e) {
		int nSize = e.getStackTrace().length;
		String msg = e.toString() + "\n";
		for (int i = 0; i < 5 && i < nSize; i++)
			msg += e.getStackTrace()[i].toString() + "\n";
		return msg;
	}

	public static String substring(String txt, int beginIndex, int endIndex) {
		if (txt == null)
			return null;
		int nLen = txt.length();
		if (beginIndex < 0 || beginIndex >= nLen || endIndex < 0 || beginIndex > endIndex)
			return null;
		if (endIndex > nLen)
			endIndex = nLen;
		return txt.substring(beginIndex, endIndex);
	}
}
